package com.spzx.product.controller;

import com.spzx.common.core.web.domain.AjaxResult;
import com.spzx.common.core.web.page.TableDataInfo;
import com.spzx.common.security.utils.SecurityUtils;
import com.spzx.product.domain.ProductSpec;
import com.spzx.product.service.ProductSpecService;
import com.spzx.product.service.ProductUnitService;
import com.spzx.product.vo.CategoryBrandVo;
import com.spzx.product.vo.ProductSpecVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.spzx.common.core.web.controller.BaseController;

import java.util.List;

/**
 * <p>
 * 商品规格 前端控制器
 * </p>
 *
 * @author lynn
 * @since 2025-03-12
 */
@RestController
@RequestMapping("/productSpec")
@Tag(name = "商品规格")
public class ProductSpecController extends BaseController {
    @Autowired
    ProductSpecService productSpecService;

    @GetMapping("/list")
    @Operation(summary = "查询商品规格列表",description = "根据商品规格名称查询商品规格列表")
    public TableDataInfo list(@RequestParam(value = "categoryId",required = false) Integer categoryId, @RequestParam(value = "specName",required = false) String specName){
        startPage(); //分页
        List<ProductSpecVo> list = productSpecService.selectProductSpecList(categoryId,specName);
        return getDataTable(list);
    }

    @Operation(summary = "新增商品规格")
    @PostMapping
    public AjaxResult add(
            @Parameter(description = "商品规格")
            @RequestBody ProductSpec productSpec) {
        productSpec.setCreateBy(SecurityUtils.getUsername());
        return toAjax(productSpecService.save(productSpec));
    }


    @Operation(summary = "删除商品规格")
    @DeleteMapping("/{ids}")
    public AjaxResult remove(
            @Parameter(description = "商品规格id列表", example = "[1,2]")
            @PathVariable List<Long> ids) {
        return toAjax(productSpecService.removeBatchByIds(ids));
    }


    /**
     * 根据分类id查询商品规格列表
     */
    @GetMapping("/productSpecList/{categoryId}")
    @Operation(summary = "根据分类id查询商品规格列表")
    public AjaxResult productSpecList(
            @Parameter(description = "分类id")
            @PathVariable Integer categoryId) {
        List<ProductSpecVo> list = productSpecService.selectProductSpecList(categoryId,null);
        return AjaxResult.success(list);
    }
}
